package com.kc.sjwh.action;

import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.entity.po.Jqyjzb;
import com.kc.sjwh.other.AjaxRes;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.other.QueryParam;
import com.kc.sjwh.service.JqyjzbService;

/**
 * 警情预警指标Action
 * @author zhouy
 *
 */
@Controller
@RequestMapping("jqyjzb")
public class JqyjzbAction extends BaseAction{
	static Logger logger = Logger.getLogger(CzllzcyAction.class);
	
	@Autowired
	JqyjzbService jqyjzbService;
	
	/**
	 * 列表页
	 * @param req
	 * @return
	 */
	@RequestMapping("list")
	public String listPage(HttpServletRequest req){
		return "jqyjzb/jqyjzbList";
	}
	
	@RequestMapping("form")
	public String form(HttpServletRequest req, String id){
		req.setAttribute("act", id==null?"新增":"修改");
		Jqyjzb one = new Jqyjzb();
		if(null != id){
			one = jqyjzbService.getOneInfo(id);
		}
		req.setAttribute("obj", one);
		return "jqyjzb/jqyjzbForm";
	}
	
	/**
	 * 获得列表数据
	 * @param req
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getData")
	public String getData(HttpServletRequest req, int pageSize, int pageNum){
		QueryParam p = new QueryParam(pageSize, pageNum);
		p.addSqlCondition(" t.sybz = 'Y' ");
		String zbname = req.getParameter("zbname");
		if(null != zbname){
			zbname = zbname.trim();
			if(!"".equals(zbname)){
				p.addSqlCondition(" t.zbmc like '%" + zbname + "%' or t.zbnr like '%" + zbname + "%' ");
			}
		}
		p.setOrder(" t.gxsj desc, t.bh asc");
		Page<Jqyjzb> page = jqyjzbService.getData(p);
//		Table table = new Table();
//		table.add(new Tr("指标ID", "创建时间", "指标名称", "指标内容", "更新时间", "操作"));//表头
//		for(Jqyjzb one : page.getList()){
//			table.add(new Tr(
//					new Td(one.getZbid()),
//					new Td(new SimpleDateFormat("yyyy/MM/dd HH:mm").format(one.getCjsj())),
//					new Td(one.getZbmc()),
//					new Td(one.getZbnr()),
//					new Td(new SimpleDateFormat("yyyy/MM/dd HH:mm").format(one.getGxsj())),
//					new Td("<a  data-toggle='modal' data-iframe='form?id="+one.getZbid()+"' data-size='lg'>修改</a> &nbsp; " + 
//							//"<a href='form?id="+one.getZbid()+"'>修改</a> &nbsp; "+ 
//							"<a onclick=\"deleteData('"+one.getZbid()+"')\">删除</a>")
//			));
//		}
//		Map data = new HashMap();
//		data.put("total", page.getTotal());
//		data.put("html", table.toString());
		return JSON.toJSONString(new AjaxRes(page));
	}
	
	/**
	 * 逻辑删除数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String deleteData(HttpServletRequest req){
		Jqyjzb one = new Jqyjzb();
		one.setZbid(req.getParameter("zbid"));
		one.setSybz("N");
		jqyjzbService.editData(one);
		return JSON.toJSONString(new AjaxRes());
	}
	
	/**
	 * 编辑 or 新增 数据
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("save")
	public String save(HttpServletRequest req){
		String type = req.getParameter("czType");
		Jqyjzb one = new Jqyjzb();
		if("add".equals(type)){
			String bh = UUID.randomUUID().toString().replace("-", "");
			one.setBh(bh);
			one.setZbid("E" + jqyjzbService.getNewZbid());
			one.setZbmc(req.getParameter("zbmc"));
			one.setZbnr(req.getParameter("zbnr"));
			jqyjzbService.addData(one);
		}else{
			one.setZbid(req.getParameter("zbid"));
			one.setZbmc(req.getParameter("zbmc"));
			one.setZbnr(req.getParameter("zbnr"));
			one.setGxsj(new Date());
			jqyjzbService.editData(one);
		}
		return JSON.toJSONString(new AjaxRes());
	}
}
